package cn.szzsi.platform.controller.admin;

import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.common.util.IdGen;
import cn.szzsi.model.Member;
import cn.szzsi.model.mall.ProductCategory;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Date;
import java.util.List;

public class AgentController extends Controller{
    public void list(){
        render(CommonUtil.TEMPLATE_PATH + "/platform/agent/agent_list.html");
    }
    public void ajaxlist() {
        List<Member> lpa = Member.dao.find("select * from sys_member where agentStates=2 and ( type=0 or type=2) order by agentApplyDate desc");
        renderJson(lpa);
        //renderNull();
    }

    public void add(){
        render(CommonUtil.TEMPLATE_PATH + "/platform/agent/agent_input.html");
    }

    public void save(){
        String agentCode = IdGen.get().nextId(10);
        String regPhone = "^0?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[57])[0-9]{8}$" ; //手机正则表达式
        String phone=getPara("phone");
        String name=getPara("name");
        String password=getPara("password");
        Integer agentLevel=getParaToInt("agentLevel",1);
        String remark=getPara("remark");
        if(StrKit.isBlank(password)){
            password=phone;
        }
        /*if(!phone.matches(regPhone)){
            renderJson(Ret.fail("info","请输入正确的手机号"));
            return;
        }*/
        Member member=Member.dao.getByPhone(phone);
        if(member==null){////既不会代理商,也不是商户
            member=new Member();
            member.setId(StrKit.getRandomUUID());
            member.setPassword(DigestUtils.md5Hex(password));
            member.setRealName(name);
            member.setType(2);
            member.setAgentLevel(agentLevel);
            member.setAgentStates(2);
            member.setRoleId("super");
            member.setBShoprank("super");
            member.setRemark(remark);
            member.setCreateDate(new Date());
            member.setAgentCode(agentCode);
            member.setAgentApplyDate(new Date());
            member.save();
        }else{
            Integer type=member.getType();
            if(0==type || 2==type){
                renderJson(Ret.fail("info","该手机号已经是代理商!"));
                return;
            }else if(1==type){//商户增加代理商身份
                member.setType(0);
                member.setAgentStates(2);
                member.setAgentLevel(agentLevel);
                member.setRemark(remark);
                member.setRealName(name);
                member.setAgentCode(agentCode);
                member.setAgentApplyDate(new Date());
                member.update();
            }
        }
        renderJson(Ret.ok("info","保存成功!").set("url","list"));
    }
    public void edit(){
        String id=getPara("id");
        Member member=Member.dao.findById(id);
        setAttr("member",member);
        render(CommonUtil.TEMPLATE_PATH + "/platform/agent/agent_input.html");
    }

    public void update(){
        String id=getPara("id");
        String realName=getPara("name");
        String password=getPara("password");
        Integer agentLevel=getParaToInt("agentLevel",1);
        String remark=getPara("remark");
        Member member=Member.dao.findById(id);
        member.setAgentLevel(agentLevel);
        member.setRealName(realName);
        member.setRemark(remark);
        if(StrKit.notBlank(password)){
            member.setPassword(DigestUtils.md5Hex(password));
        }
        member.update();
        System.out.println("-------------------------------");
        renderJson(Ret.ok("info","更新成功!").set("url","list"));
    }
}
